import {ShapeFlags} from "../shared/ShapeFlags";

/**
 *  初始化插槽
 * @param instance  虚拟节点实例,
 * @param children  组件的自己点
 */
export function initSlots(instance, children) {
    // instance.slots = Array.isArray(children) ? children : [children];

    //  从组件实例上取出 虚拟节点
    const {vnode} = instance;
    // 使用&判断虚拟节点需不需要处理插槽。如果&位运算后的结果是大于0的,则会被强转成true,如果是小于0的则会被强转成false
    if (vnode.shapeFlags & ShapeFlags.SLOT_CHILDREN) {
        const slots = {};
        for (const key in children) {
            const value = children[key];
            slots[key] = (paops) => normalizeSlotValue(value(paops))
        }
        instance.slots = slots;
    }
}

function normalizeSlotValue(value) {
    return Array.isArray(value) ? value : [value];
}